<script setup>
import { ref, computed } from "vue";
import { usePermissionStore } from '@/stores/permission';
import { filterAvailableCards } from "../../../../config/cardConfig";

const props = defineProps({
  modelValue: Boolean,
});

const visible = computed({
  get: () => props.modelValue,
  set: (value) => emit("update:modelValue", value),
});

const emit = defineEmits(["update:modelValue", "addComponent"]);

const permissionStore = usePermissionStore();

// 直接使用过滤函数获取可用卡片
const availableCards = computed(() => {
  return filterAvailableCards(permissionStore.userPermissions);
});

// 按图表类型分组
const groupedCards = computed(() => {
  const groups = {
    '趋势图': [],
    '饼图': [],
    '柱状图': [],
    '表格': [],
    '排名图': []
  };

  if (!availableCards.value) {
    return groups;
  }

  availableCards.value.forEach(card => {
    if (card.icon.includes('line-chart')) {
      groups['趋势图'].push(card);
    } else if (card.icon.includes('pie-chart')) {
      groups['饼图'].push(card);
    } else if (card.icon.includes('bar-chart')) {
      groups['柱状图'].push(card);
    } else if (card.icon.includes('table')) {
      groups['表格'].push(card);
    } else if (card.icon.includes('sort-amount-desc')) {
      groups['排名图'].push(card);
    }
  });

  return groups;
});

// 添加组件方法
const handleAddComponent = (component) => {
  emit("addComponent", component);
};
</script>

<template>
  <el-drawer :lock-scroll="false" v-model="visible" title="添加图表" :with-header="true"
    @update:modelValue="emit('update:modelValue', $event)">
    <div class="cards-container">
      <template v-for="(cards, type) in groupedCards" :key="type">
        <div v-if="cards && cards.length > 0" class="card-group">
          <h3 class="group-title">{{ type }}</h3>
          <el-card v-for="card in cards" :key="card.component" class="card-item">
            <div class="card-content">
              <i :class="card.icon" class="card-icon"></i>
              <div class="card-info">
                <h4>{{ card.name }}</h4>
                <p>{{ card.description }}</p>
              </div>
              <el-button type="primary" size="small" @click="handleAddComponent(card.component)" class="add-button">
                添加
              </el-button>
            </div>
          </el-card>
        </div>
      </template>
    </div>
  </el-drawer>
</template>

<style scoped>
.cards-container {
  display: flex;
  flex-direction: column;
  gap: 24px;
  padding: 16px;
}

.card-group {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.group-title {
  margin: 0;
  font-size: 16px;
  font-weight: 500;
  color: var(--el-text-color-primary);
  padding-bottom: 8px;
  border-bottom: 1px solid var(--el-border-color-light);
}

.card-item {
  transition: all 0.3s;
}

.card-item:hover {
  transform: translateX(4px);
  border-color: var(--el-color-primary);
}

.card-content {
  display: flex;
  align-items: center;
  gap: 16px;
}

.card-icon {
  font-size: 24px;
  color: var(--el-color-primary);
  width: 24px;
  text-align: center;
}

.card-info {
  flex: 1;
}

.card-info h4 {
  margin: 0 0 4px 0;
  color: var(--el-text-color-primary);
}

.card-info p {
  margin: 0;
  font-size: 12px;
  color: var(--el-text-color-secondary);
}

.add-button {
  flex-shrink: 0;
}
</style>
